from flask import abort
from flask import current_app
from flask import g, jsonify
from flask import request
from flask import session
from info import db
from info.utlis.response_code import RET
from . import profile_bp
from flask import render_template
from info.utlis.common import login_user_data
from info.utlis.image_store import qiniu_image_store
from info import constants
from info.models import User, Category, News


@profile_bp.route('/other_news_list')
def other_news_list():
    # 获取页数
    p = request.args.get("p", 1)
    user_id = request.args.get("user_id")
    try:
        p = int(p)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")

    if not all([p, user_id]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")

    try:
        user = User.query.get(user_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="数据查询错误")

    if not user:
        return jsonify(errno=RET.NODATA, errmsg="用户不存在")

    try:
        paginate = News.query.filter(News.user_id == user.id).paginate(p, constants.OTHER_NEWS_PAGE_MAX_COUNT, False)
        # 获取当前页数据
        news_li = paginate.items
        # 获取当前页
        current_page = paginate.page
        # 获取总页数
        total_page = paginate.pages
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="数据查询错误")

    news_dict_li = []

    for news_item in news_li:
        news_dict_li.append(news_item.to_review_dict())
    data = {"news_list": news_dict_li, "total_page": total_page, "current_page": current_page}
    return jsonify(errno=RET.OK, errmsg="OK", data=data)


@profile_bp.route('/other_info')
@login_user_data
def other_info():
    user = g.user
    # 获取其他用户id
    user_id = request.args.get("user_id")
    if not user_id:
        abort(404)
    # 查询用户模型
    other = None
    try:
        other = User.query.get(user_id)
    except Exception as e:
        current_app.logger.error(e)
    if not other:
        abort(404)

    # 判断当前登录用户是否关注过该用户
    is_followed = False
    if g.user:
        if other.followers.filter(User.id == user.id).count() > 0:
            is_followed = True

    # 组织数据，并返回
    data = {
        "user_info": user.to_dict(),
        "other_info": other.to_dict(),
        "is_followed": is_followed
    }

    return render_template("news/other.html", data=data)


@profile_bp.route('/user_follow')
@login_user_data
def user_followed_list():
    """当前用户关注列表"""
    # 1.获取参数
    p = request.args.get("p", 1)
    user = g.user
    # 2.参数校验
    try:
        p = int(p)
    except Exception as e:
        current_app.logger.error(e)
        p = 1
    # 3.逻辑处理
    # lazy="dynamic"如果真正用到里面的值，就会给你返回一个列表数据，如果只是简单的查询就会返回一个查询对象给你
    user_list = []
    current_page = 1
    total_page = 1
    if user:
        try:
            # 指明了lazy=dynamic
            # user.followed 如果取直接使用 当前用户的偶像列表
            # user.followed 如果取查询 查询对象
            paginate = user.followed.paginate(p, constants.USER_COLLECTION_MAX_NEWS, False)
            user_list = paginate.items
            current_page = paginate.page
            total_page = paginate.pages
        except Exception as e:
            current_app.logger.error(e)

    # 用户对象列表转成字典列表
    user_dict_list = []
    for user in user_list if user_list else []:
        user_dict_list.append(user.to_dict())

    data = {
        "users": user_dict_list,
        "current_page": current_page,
        "total_page": total_page
    }
    # 4.返回值
    return render_template("news/user_follow.html", data=data)



# /user/collection?p=1
@profile_bp.route('/news_list')
@login_user_data
def news_list():
    """用户收藏的列表展示"""
    #1.获取参数
    p = request.args.get("p", 1)
    user = g.user
    # 2.参数校验
    try:
        p = int(p)
    except Exception as e:
        current_app.logger.error(e)
        p = 1
    # 3.逻辑处理
    # lazy="dynamic"如果真正用到里面的值，就会给你返回一个列表数据，如果只是简单的查询就会返回一个查询对象给你
    news_list = []
    current_page = 1
    total_page = 1
    if user:
        try:
            paginate = News.query.filter(News.user_id == user.id).\
                paginate(p, constants.USER_COLLECTION_MAX_NEWS, False)
            news_list = paginate.items
            current_page = paginate.page
            total_page = paginate.pages
        except Exception as e:
            current_app.logger.error(e)

    # 新闻对象列表转成字典列表
    news_dict_list = []
    for news in news_list if news_list else []:
        news_dict_list.append(news.to_review_dict())

    data = {
        "news_list": news_dict_list,
        "current_page": current_page,
        "total_page": total_page
    }
    # 4.返回值
    return render_template("news/user_news_list.html", data=data)


@profile_bp.route('/news_release', methods=['POST', 'GET'])
@login_user_data
def news_release():
    """展示新闻发布页面 发布新闻的后端逻辑"""
    if request.method == "GET":
        try:
            categories = Category.query.all()
        except Exception as e:
            current_app.logger.error(e)
        # 分类对象列表转成字典列表
        category_dict_list = []
        for category in categories if categories else []:
            category_dict_list.append(category.to_dict())
        # 注意点：需要移除最新分类
        category_dict_list.pop(0)
        return render_template("news/user_news_release.html",data={"categories":category_dict_list})

    #POST请求发布新闻
    #1.获取参数
    title = request.form.get("title")
    category_id = request.form.get("category_id")
    digest = request.form.get("digest")
    index_image = request.files.get("index_image")
    content = request.form.get("content")
    source = "个人发布"
    user = g.user
    #2.参数校验
    # 2.参数校验
    if not all([title, category_id, digest, index_image, content]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数不足")

    if not user:
        return jsonify(errno=RET.SESSIONERR, errmsg="用户未登录")
    #3.逻辑处理
    # 获取图片二进制数据
    image_data = index_image.read()
    try:
        image_name = qiniu_image_store(image_data)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR, errmsg="上传图片到七牛云失败")
    # 创建新闻模型
    news = News()
    news.title = title
    news.category_id = category_id
    news.digest = digest
    news.index_image_url = constants.QINIU_DOMIN_PREFIX + image_name
    news.content = content
    news.source = source
    news.user_id = user.id
    # 新闻处于审核中
    news.status = 1

    # 保存到数据库
    try:
        db.session.add(news)
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
        return jsonify(errno=RET.DBERR, errmsg="保存新闻对象到数据库异常")
    #4.返回值处理
    return jsonify(errno=RET.OK, errmsg="新闻发布成功")


# /user/collection?p=1
@profile_bp.route('/collection')
@login_user_data
def user_collection():
    """用户收藏的列表展示"""
    #1.获取参数
    p = request.args.get("p", 1)
    user = g.user
    # 2.参数校验
    try:
        p = int(p)
    except Exception as e:
        current_app.logger.error(e)
        p = 1
    # 3.逻辑处理
    # lazy="dynamic"如果真正用到里面的值，就会给你返回一个列表数据，如果只是简单的查询就会返回一个查询对象给你
    news_list = []
    current_page = 1
    total_page = 1
    if user:
        try:
            paginate = user.collection_news.paginate(p, constants.USER_COLLECTION_MAX_NEWS, False)
            news_list = paginate.items
            current_page = paginate.page
            total_page = paginate.pages
        except Exception as e:
            current_app.logger.error(e)

    # 新闻对象列表转成字典列表
    news_dict_list = []
    for news in news_list if news_list else []:
        news_dict_list.append(news.to_review_dict())

    data = {
        "collections": news_dict_list,
        "current_page": current_page,
        "total_page": total_page
    }
    # 4.返回值
    return render_template("news/user_collection.html", data=data)


@profile_bp.route('/pass_info', methods=['POST', 'GET'])
@login_user_data
def pass_info():
    """展示修改密码页面 修改密码后端逻辑"""
    if request.method == "GET":
        return render_template("news/user_pass_info.html")

    # post请求修改密码
    #1.参数获取
    old_password = request.json.get("old_password")
    new_password = request.json.get("new_password")
    user = g.user

    #2.参数校验
    if not all([old_password, new_password]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数不足")

    if not user:
        return jsonify(errno=RET.SESSIONERR, errmsg="用户未登录")
    #3.逻辑处理
    if not user.check_passowrd(old_password):
        return jsonify(errno=RET.PARAMERR, errmsg="旧密码填写错误")
    #将新密码赋值给password属性
    user.password = new_password

    try:
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="保存用户对象异常")
    #4.返回值
    return jsonify(errno=RET.OK, errmsg="修改密码成功")


@profile_bp.route('/pic_info', methods=['POST', 'GET'])
@login_user_data
def pic_info():
    """展示用户头像页面 修改用户头像接口"""
    if request.method == "GET":
        return render_template("news/user_pic_info.html")

    # POST获取用户上传的图片二进制数据上传到七牛云
    avatar_data = request.files.get("avatar").read()
    user = g.user

    # 校验参数
    if not avatar_data:
        return jsonify(errno=RET.NODATA, errmsg="图片数据不能为空")
    if not user:
        return jsonify(errno=RET.SESSIONERR, errmsg="用户未登录")
    # 逻辑处理
    try:
        # 调用封装好的方法将图片上传到七牛云
        image_name = qiniu_image_store(avatar_data)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR, errmsg="上传图片到七牛云失败")
    # 只将图片名称存储，防止后期修改七牛云域名
    user.avatar_url = image_name

    try:
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
        return jsonify(errno=RET.DBERR, errmsg="保存用户url失败")

    #4.组织响应对象
    full_url = constants.QINIU_DOMIN_PREFIX + image_name
    data = {
        "avatar_url": full_url

    }
    return jsonify(errno=RET.OK, errmsg="上传图片到七牛云成功", data=data)


# GET: /user/base_info
@profile_bp.route('/base_info', methods=["POST", "GET"])
@login_user_data
def base_info():
    """获取用户基本资料页面、修改用户基本资料页面"""
    user = g.user
    if request.method == 'GET':
        data = {
            "user_info": user.to_dict() if user else None,
        }
        return render_template("news/user_base_info.html",data=data)

    # POST请求 修改用户基本资料
    #1.获取参数
    params_dict = request.json
    nick_name = params_dict.get("nick_name")
    signature = params_dict.get("signature")
    gender = params_dict.get("gender")

    #  2.1 非空判断
    if not all([nick_name, signature, gender]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数不足")

    # 2.2 用户是否登录判断
    if not user:
        return jsonify(errno=RET.SESSIONERR, errmsg="用户未登录")

    if gender not in ["MAN", "WOMAN"]:
        return jsonify(errno=RET.PARAMERR, errmsg="action数据填写错误")

    #3.逻辑处理
    user.nick_name = nick_name
    user.signature = signature
    user.gender = gender
    # 更新session中的nick_name数据
    session["nick_name"] = nick_name

    # 将修改操作保存回数据库
    try:
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
        return jsonify(errno=RET.DBERR, errmsg="修改用户数据异常")

    #4.返回值处理
    return jsonify(errno=RET.OK, errmsg="修改用户数据成功")


# /user/info
@profile_bp.route('/info')
@login_user_data
def user_info():
    """返回用户个人中心页面"""
    user = g.user
    data = {
        "user_info": user.to_dict() if user else None,
    }
    return render_template("news/user.html", data=data)